package com.banmao.mall.realm;

import com.banmao.mall.mapper.SystemMapper;
import com.banmao.mall.model.pojo.Admin;
import com.banmao.mall.service.admin.AdminService;
import com.banmao.mall.service.admin.MarketService;
import com.banmao.mall.service.admin.SystemService;
import com.banmao.mall.service.admin.UserService;
import com.banmao.mall.util.ShiroMd5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.List;

/**
 *  管理员Realm
 */
@Component("adminRealm")
public class AdminRealm extends AuthorizingRealm {

    @Autowired
    @Lazy // 防止Spring事务注解失效
    AdminService adminService;
    @Autowired
    SystemService systemService;
    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String  username = (String) session.getAttribute("adminUsername");

//        Admin primaryPrincipal = (Admin) principalCollection.getPrimaryPrincipal();
//        String username = primaryPrincipal.getUsername();
//        String username = (String) principalCollection.getPrimaryPrincipal();
        List<String> permission  = systemService.selectPermissionByUsername(username);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permission);
        return simpleAuthorizationInfo;

    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;

        // 调用Service层 连接数据库
        Admin userLogin = adminService.login(userToken.getUsername());

        if(userLogin == null) {
            // 没有这个用户
            return null;
        }


        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("adminId", userLogin.getId());
        session.setAttribute("adminUsername",userLogin.getUsername());
        // 从数据库中取该用户的password
        String password = userLogin.getPassword();

        // 进行加密算法
        char[] userTokenpasswordChar = userToken.getPassword();
        String userTokenPassword = String.valueOf(userTokenpasswordChar);
        userTokenPassword = ShiroMd5Util.SysMd5(userLogin.getUsername(), userTokenPassword);
        userToken.setPassword(userTokenPassword.toCharArray());

        // 创建SimpleAuthenticationInfo
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("", password, this.getName());

        // 返回 return的过程完成password的验证
        return simpleAuthenticationInfo;
    }

}
